<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class OrderController extends Controller
{
    //订单管理查询
    public function manage(Request $request)
    {
    	// dd($request);
    	// $orders = DB::table('orders') -> get();
        $data = DB::table('orders')
         	->join('users','users.id','=','orders.user_id')//关联用户表 和 订单表中的user_id
         	->join('category','category.category_id','=','orders.type_id')//关联category表的category_id与roders表中的type_id
         	->join('goods','goods.id','=','orders.goods_id')//关联goods表的id与roders表中的goods_id
         	->select('users.user_name', 'category.category_name','goods.goods_name','goods.goods_price','orders.orders_price','orders.id','orders.orders_id','orders.orders_status','orders.orders_pay','goods.goods_picture')
         	->where('orders_id', 'like', '%' . $request -> input('keywords') . '%')
         	->paginate($request -> input('num',4));				
    	return view('admin.order.order', ['title' => '订单管理', 'data' => $data]);
    }

    //移除订单  改变订单状态是否显示
    public function out($id)
    {
    	
        // $data = DB::table('users') -> where('id', $id) -> first();
        // return view('admin.order.', ['data' => $data, 'title' => '用户编辑']);
        // 
        dd($id);
        $res = DB::table('order') -> insert($id);
    }

    public function ajaxStatus(Request $request)
	{
		$id = $request -> input('id');
		dd($id);
		//查询
		$status = DB::table('users') -> where('id', $id) -> first() -> status;
		
		//判断
		if($status == 0)
		{
			$res = DB::table('users') -> where('id', $id) -> update(['status' => 1]);
			
			if($res)
			{
				return 1;
			}else
			{
				return 2;
			}
		}elseif($status == 1)
		{
			$res = DB::table('users') -> where('id', $id) -> update(['status' => 0]);
			if($res)
			{
				return 0;
			}else
			{
				return 2;
			}
		}
	}
}
